// packageC/pages/package/agv_manage/getCode/getCode.js
const mybehavior = require("../../../../../behaviors/requests")
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    baseUrl: "https://logisticsengineering.gree.com/agv_dispatch_system",
    options1: [],
    options2: ['是', '否'],

    data_list: [],

    current: 1,
    page_size: 5,
    department: "",
    material_name: "",
    special_agv_list: "",
    page_all: 0,
    total: 0,
    caozuo_show: false,
    change_agv_name: "",
    current_data: {},
    instruct_map: [
      { "instruct_code": 0, "instruct": "无动作" },
      { "instruct_code": 1, "instruct": "停车" },
      { "instruct_code": 2, "instruct": "左转" },
      { "instruct_code": 3, "instruct": "右转" },
      { 'instruct_code': 4, 'instruct': "高速" },
      { "instruct_code": 5, "instruct": "中速" },
      { "instruct_code": 6, "instruct": "低速" },
      { "instruct_code": 7, "instruct": "临停" },
    ],
    current_post_list: [],
    post_show: false,
    card_show: false,
    options_storage: [],
    current_storage_list:[]


  },
  // 途径货架代码获取
  showStorageName(e) {
    // console.log(e.currentTarget.dataset.row_data);
    let key_arr = e.currentTarget.dataset.row_data
    let storage_rack_id_list = []
    let arr = JSON.parse(JSON.stringify(this.data.options_storage))
    // console.log(arr);
    for (let i = 0; i < arr.length; i++) {
      for (let j = 0; j < key_arr.length; j++) {
        if (key_arr[j] == arr[i].key) {
          storage_rack_id_list.push(arr[i].storage_rack_name)
        }
      }
    }
    this.setData({
      current_storage_list:storage_rack_id_list,
      storage_show:true
    })


  },

    // 岗位站点关闭
    confirmStorage() {
      this.setData({
        storage_show: false,
        current_storage_list: []
      })
    },
  // 清空筛选条件
  clearSearchDatas() {
    this.setData({
      department: "",
      material_name: "",
    })
    this.getMagneticData()
  },
  toAddLine() {
    wx.navigateTo({
      url: '/packageC/pages/package/agv_manage/lineManage/addLine/addLine',
    })
  },
  // 删除数据
  onDelete(e) {
    // console.log(e.target.dataset.row_data);
    wx.showModal({
      title: '删除',
      content: '确认删除？',
      complete: (res) => {
        if (res.cancel) {
        }
        if (res.confirm) {
          let url = this.data.baseUrl + "/delete_magnetic_agv_info"
          let data = {
            magnetic_route_id: e.target.dataset.row_data.key,
          }
          // console.log("删除数据-传过去的数据", data);
          this.post(url, data).then(res => {
            if (res.code == 20000) {
              wx.showToast({
                title: res.msg,
                icon: 'none'
              })
              this.getMagneticData()

            } else {
              wx.showToast({
                title: res.msg,
                icon: 'error'
              })
            }
          }).catch(err => {
            console.log(err);
            wx.showToast({
              title: err.msg,
              icon: 'error'
            })
          })
        }
      }
    })


  },
  // 编辑数据
  updateData() {
    if (this.data.change_agv_name == "") {
      wx.showToast({
        title: '输入内容为空！',
        icon: 'error'
      })
    } else {
      let url = this.data.baseUrl + "/update_magnetic_agv_info"
      let data = {
        agv_num: this.data.current_data.agv_num,
        agv_name: this.data.change_agv_name,
        department: this.data.current_data.department,
      }
      // console.log("编辑数据-传过去的数据", data);
      this.post(url, data).then(res => {
        if (res.code == 20000) {
          this.setData({
            caozuo_show: false,
            current_data: {},
            change_agv_name: ""
          })
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
          this.getMagneticData()
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'error'
          })
        }
      }).catch(err => {
        console.log(err);
        wx.showToast({
          title: err.msg,
          icon: 'error'
        })
      })

    }
  },
  // 编辑
  editBtn(e) {
    // console.log(e.target.dataset.row_data);
    wx.navigateTo({
      url: '/packageC/pages/package/agv_manage/lineManage/editLine/editLine',
      success(res) {
        res.eventChannel.emit('acceptDataFromOpenerPage', e.target.dataset.row_data)

      }
    })
  },
  onCzCancel() {
    this.setData({
      caozuo_show: false,
      current_data: {},
      change_agv_name: ""
    })
    // console.log("当前数据", this.data.current_data);
  },
  // 分页
  onPageChange(e) {
    // console.log("xxx",e.detail.sonParam);
    let p = e.detail.sonParam
    this.setData({
      current: p,
    })
    this.getMagneticData()
  },
  // 下拉选择
  getPickerVlaue1(e) {
    let key = e.target.id
    // console.log(e);
    this.setData({
      [key]: e.detail.name

    })
  },
  // 下拉选择
  getPickerVlaue(e) {
    let key = e.target.id
    // console.log(e);
    this.setData({
      [key]: e.detail

    })
  },

  // 查询表格数据
  getMagneticData() {
    let url = this.data.baseUrl + "/get_magnetic_route"
    let data = {
      department: this.data.department,
      material_name: this.data.material_name,
      special_agv_list: this.data.special_agv_list,
      current: this.data.current,
      page_size: this.data.page_size,
    }
    // console.log("传过去的数据", data);
    this.get(url, data).then(res => {
      if (res.code == 20000) {
        this.setData({
          data_list: res.data.table_data,
          page_all: Math.ceil(res.data.table_total / this.data.page_size),
          total: res.data.table_total,
        })

      } else {
        this.setData({
          data_list: [],
          page_all: 0,
          total: 0,
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        data_list: [],
        page_all: 0,
        total: 0,
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },
  // 查询途径货架信息
  getStorageData() {
    let url = this.data.baseUrl + "/get_storage_rack_station"
    let data = {
      department: this.data.department,
      current: 1,
      page_size: 999,
    }
    this.get(url, data).then(res => {
      if (res.code == 20000) {
        let name_list = []
        for (let i = 0; i < res.data.table_data.length; i++) {
          name_list.push(res.data.table_data[i].storage_rack_name)
        }
        this.setData({
          options_storage: res.data.table_data,
          options_storage_name: name_list,
        })
      } else {
        this.setData({
          options_storage: [],
          options_storage_name: [],
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        options_storage: [],
        options_storage_name: [],
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },
  // 查询岗位站点信息
  getPostData() {
    let url = this.data.baseUrl + "/get_post_station"

    let data = {
      department: "",
      current: 1,
      page_size: 999,
    }
    this.get(url, data).then(res => {
      // console.log("查询岗位站点信息", res);
      if (res.code == 20000) {
        this.setData({
          post_options: res.data.table_data,
        })
      } else {
        this.setData({
          post_options: [],
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        post_options: [],
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },
  // 查询小车信息
  getAgvtData() {
    let url = this.data.baseUrl + "/get_magnetic_agv_info"

    let data = {
      department: "",
    }
    this.get(url, data).then(res => {
      if (res.code == 20000) {
        this.setData({
          options2: res.data.table_data,
        })
      } else {
        this.setData({
          options2: [],
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        options2: [],
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },

  // 查询部门信息
  getdepartmentData() {
    let url = this.data.baseUrl + "/get_department_list"
    let data = {

    }
    this.get(url, data).then(res => {
      if (res.code == 20000) {
        // console.log(res.data);
        this.setData({
          options1: res.data,
        })
      } else {
        this.setData({
          options1: [],
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        options1: [],
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },
  // 查询按钮
  getMagneticDataBtn() {
    this.setData({
      current: 1,
      page_size: 5,
    })
    this.getMagneticData()
  },

  // 岗位站点显示
  showPost(e) {
    // console.log(e.currentTarget.dataset.row_data);
    let array = e.currentTarget.dataset.row_data
    let post_list = JSON.parse(JSON.stringify(this.data.post_options))
    let show_list = []
    for (let i = 0; i < array.length; i++) {
      for (let j = 0; j < post_list.length; j++) {
        if (array[i] == post_list[j].key) {
          show_list.push(post_list[j])
        }
      }
    }
    // console.log(show_list);
    this.setData({
      post_show: true,
      current_post_list: show_list
    }
    )
  },
  // 岗位站点关闭
  confirmPost() {
    this.setData({
      post_show: false,
      current_post_list: []
    })
  },
  // 功能卡显示
  showFCard(e) {
    // console.log(e.currentTarget.dataset.row_data);
    let instruct_map = JSON.parse(JSON.stringify(this.data.instruct_map))
    let array = e.currentTarget.dataset.row_data
    for (let i = 0; i < array.length; i++) {
      for (let j = 0; j < instruct_map.length; j++) {
        if (array[i].instruct == instruct_map[j].instruct_code) {
          array[i].instruct = instruct_map[j].instruct
        }

      }
    }
    this.setData({
      card_show: true,
      current_card_list: array
    }
    )
  },
  // 功能卡关闭
  confirmCard() {
    this.setData({
      card_show: false,
      current_card_list: []
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  // 生成小程序码

  onLoad(options) {
    // this.getMagneticData()
    // this.getStorageData()
    // this.getPostData()
    // this.getAgvtData()
    // this.getdepartmentData()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getMagneticData()
    this.getStorageData()
    this.getPostData()
    this.getAgvtData()
    this.getdepartmentData()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})